package com.gan.simple_init.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gan.simple_init.common.exception.BusinessException;
import com.gan.simple_init.model.sys.entity.SysRole;
import com.gan.simple_init.model.sys.vo.SysRoleVO;
import com.gan.simple_init.service.SysRoleService;
import com.gan.simple_init.mapper.SysRoleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gbw
 * @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
 * @createDate 2023-08-11 14:47:38
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {

    @Resource
    SysRoleMapper roleMapper;

    @Override
    public List<SysRoleVO> selectRoleNameList() {
        return roleMapper.selectRoleNameList();
    }

    @Override
    public boolean updateRoleById(Long userId, Long roleId) {
        List<Long> roleIdList = roleMapper.selectRoleNameList().stream().map(SysRoleVO::getRoleId).collect(Collectors.toList());
        if (!roleIdList.contains(roleId)){
            throw new BusinessException("没有该角色!");
        }
        return roleMapper.updateUserRole(userId, roleId) > 0;
    }
}




